import urllib3

# 从线程池中获取http客户端
http_client = urllib3.PoolManager()

# 请求地址
url = 'https://www.baidu.com'

# 请求头
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'
}

# 发送请求 获取服务器响应
# 请求方式：GET POST PUT PATCH DELETE OPTION
response = http_client.request(method="GET",url=url,headers=headers)

# 输出HTTP状态码
# print(response.status)

# 输出服务器响应字符串
# print(response.data.decode('utf-8'))

if response.status == 200:
    print('正在爬取,请稍等...')
    # 将网页保存
    with open(file='百度.html',mode='w',encoding='utf-8') as f:
        f.write(response.data.decode('utf-8'))
        f.close()
    print('爬取成功')

